﻿document.observe('dom:loaded', function() {
    if (GetCookie("EmailNhanGiaTot") && $('pMock').innerHTML != "2") {
        $('dLanding').innerHTML = "";
        $('dShadow').style.display = "none";
        $('dLanding').style.display = "none";
    } else {        
        if ($('ddlPopupEmail')) {
            loadboxEvents();
        }
        if ($("spnTimeDealLanding")) {
            TimeCountDown_Landing();
        }
        if ($('dLadingPage_1')) {
			$('dShadow').style.display = "block";
			$('dLanding').style.display = "block";
            $("dLadingPage_1").style.display = "block";
            setPopUpCenter("dLadingPage_1");
            window.onresize = function() {
                setPopUpCenter("dLadingPage_1");
                setPopUpCenter("dLadingPage_2");
                setPopUpCenter("dLadingPage_3");
            };
            $('txtEmail').focus();
            $('btn-sendEmail').onclick = SendEmailNhanGiaTot;
            $('txtEmail').onkeypress = PressKeyEmail;

        }
    }
});
function PressKeyEmail(e) {
    var evt = e ? e : window.event;
    var bt = $(this.id);
    if (bt) {
        if (evt.keyCode == "13" || evt.which == "13" || evt.charCode == "13") {
            try { bt.click(); } catch (e) { }
            switch (this.id) {
                case "txtEmail":
                    SendEmailNhanGiaTot();
                    break;
            }
            return false;
        }
    }
}
function SendEmailNhanGiaTot() {
    var strUrlLocation = window.location.href;
    strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
    var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    if ($('txtEmail').value.length == 0) {
        $("pMess").innerHTML = langMess()[0][24];
        return;
    }
    else if (checkMail($('txtEmail').value) == false) {
        $("pMess").innerHTML = langMess()[0][4];
        return;
    }

    //utils.call(strUrlLink + strStateNM + "/InBanner.aspx", { "flag": "PopupEmailNhanGiaTot", "email": $("txtEmail").value, "state": $('ddlPopupEmail').value }, "POST", "insertPopUpEmailResult", "insertPopUpEmailResult");
    utils.call(strUrlLink + "InBanner.aspx", { "flag": "PopupEmailNhanGiaTot", "email": $("txtEmail").value, "state": $('ddlPopupEmail').value, "mock": $('pMock').innerHTML }, "POST", "insertPopUpEmailResult", "insertPopUpEmailResult");

    //utils.call("http://localhost:50606/InBanner.aspx", { "flag": "PopupEmailNhanGiaTot", "email": $("txtEmail").value, "state": $('ddlPopupEmail').value }, "POST", "insertPopUpEmailResult", "insertPopUpEmailResult");
    $('dLadingPage_1').style.display = "none";
    $('dLadingPage_2').style.display = "block";
    setPopUpCenter("dLadingPage_2");
};

function insertPopUpEmailResult(str) {
    if (parseInt(str) == 1) {//insert thanh cong

        if ($('pMock').innerHTML == "2") {
            var strURL = window.location.href;
            strURL = strURL.substring(strURL.indexOf("?") + 1, strURL.length);
            try {
                strMockCity = "";
                switch ($('ddlPopupEmail').value) {
                    case "8":
                        strMockCity = "tp-ho-chi-minh";
                        break;
                    case "4":
                        strMockCity = "ha-noi";
                        break;
                    case "31":
                        strMockCity = "ha-phong";
                        break;
                    case "710":
                        strMockCity = "can-tho";
                        break;
                }

                SetCookie("stateCity", strMockCity, 120);
            } catch (ex) { }
            //_gaq.push(['_trackPageview', '/Subscriptions_result/' + $("divCity_Sub").value]);
            window.location.href = "http://www.nhommua.com/Thanks_Subscriptions.aspx?email=" + $("txtEmail").value + "&u=" + strURL;
        }
        else {
            $('SIDNhanGiaTot').innerHTML = $("txtEmail").value;
            $('dLadingPage_2').style.display = "none";
            $('dLadingPage_3').style.display = "block";
            SetCookie("EmailNhanGiaTot", "1", 60);
            setPopUpCenter("dLadingPage_3");
            TimeCountDown_Email();
        }
    }
    else {//insert that bai
        if ($('pMock').innerHTML == "2") {
            window.location.href = "http://www.nhommua.com";
        }
        else {
            $('dLadingPage_2').style.display = "none";
            $('dLadingPage_1').style.display = "none";
            ClosePopupEmail('dLadingPage_2');
            SetCookie("EmailNhanGiaTot", "1", 60);
            //setPopUpCenter("dLadingPage_1");
            //$("pMess").innerHTML = langMess()[0][27];
        }
    }
};

function loadboxEvents() {
    if (document.getElementById("ddlPopupEmail")) {
        var ddlWardPos = new ddlDD();
        ddlWardPos.initializeCtrl('ddlPopupEmail', onLoadDDL, onChangeCities, true); //edit here 28/02 2:12PM
    }
};

function onChangeCities(id, value) {
    var selectedValue = value;
    $('ddlPopupEmail_SV').innerHTML = value; //edit here 28/02 2:12PM
};

function ClosePopupEmail(idPopup) {
    if ($('pMock').innerHTML == "2"){
        $(idPopup).style.display = "none";
        $('dShadow').style.display = "none";
        SetCookie("EmailNhanGiaTot", "1", 60);
        window.location.href = "http://www.nhommua.com";
    }
    if ($(idPopup)) {
        $(idPopup).style.display = "none";
        $('dShadow').style.display = "none";
        SetCookie("EmailNhanGiaTot", "1", 60);
    }
}

var timeEmail;
var strTimeCountDownEmail = "";
var hoursEmail = 0,
	minsEmail = 0,
	secsEmail = 0;
var strDateTimeEmail = "2-29-2012 10:00 AM";
var strCurTimeEmail = "2-29-2012 09:30 AM";
var deadDayEmail = null;
var curDayEmail = null;
var remainTimeEmail = 1800000;
var timeEmail;

function TimeCountDown_Email() {

    if (remainTimeEmail > 0) {
        remainTimeEmail = remainTimeEmail - 1000;
        if (remainTimeEmail <= 0) {
            clearTimeout(timeEmail);
            strTimeCountDownEmail = "<em>00<i>phút</i></em><em>00<i>giây</i></em>"
        }
        else {
            hoursEmail = parseInt(remainTimeEmail / 3600000);
            minsEmail = parseInt((remainTimeEmail - (hoursEmail * 3600000)) / 60000);
            secsEmail = parseInt((remainTimeEmail - (hoursEmail * 3600000) - (minsEmail * 60000)) / 1000);
            //strTimeCountDown = hours + ":" + mins + ":" + secs;
            strTimeCountDownEmail = "<em>" + minsEmail + "<i>phút</i></em><em>" + secsEmail + "<i>giây</i></em>";
        }
        $("spnTimeEmail").innerHTML = strTimeCountDownEmail;
    }
    timeEmail = setTimeout("TimeCountDown_Email()", 1000);
}

var timeEmailMock;
var strTimeLanding = "";
var hoursMock = 0,
	minsMock = 0,
	secsMock = 0;
function TimeCountDown_Landing() {

    for (var i = 0; i < arrRemainTime.length; i++) {

        if (arrRemainTimeEmail[i] > 0) {
            arrRemainTimeEmail[i] = arrRemainTimeEmail[i] - 1000;
            if (arrRemainTimeEmail[i] <= 0) {
                $("spnTimeDealLanding").innerHTML = "00:00:00"
                clearTimeout(timeEmailMock);
            }
            else {
                hoursMock = parseInt(arrRemainTimeEmail[i] / 3600000);
                minsMock = parseInt((arrRemainTimeEmail[i] - (hoursMock * 3600000)) / 60000);
                secsMock = parseInt((arrRemainTimeEmail[i] - (hoursMock * 3600000) - (minsMock * 60000)) / 1000);
                strTimeLanding = hoursMock + ":" + minsMock + ":" + secsMock;
                $("spnTimeDealLanding").innerHTML = strTimeLanding;
            }
        }
    }
    timeEmailMock = setTimeout("TimeCountDown_Landing()", 1000);
}

